Obsidian タスクの開始・終了 2
< Obsidian タスクの開始・終了 || >
行頭が"- "のみの場合も、開始時刻が打刻できるように変更
プロパティの更新処理を削除
空白行、未完了タスク⇒開始時刻追加 - [ ] hh:mm- タスク名
開始時刻が入っている未完了タスク⇒終了時刻追加 + タスク完了 - [x] hh:mm-hh:mm タスク名
開始時刻と終了時刻の両方が入っている完了タスク⇒ 開始時刻・終了時刻を消して未完了タスクとしてコピー - [ ] タスク名
code:js
<%*
const now = tp.date.now("HH:mm");
// 現在のカーソル位置を取得
const initialCursorPos = this.app.workspace.activeEditor.editor.getCursor();
const currentLineText = this.app.workspace.activeEditor.editor.getLine(initialCursorPos.line); // trim() を削除
// 行が空白の場合、新しいタスクを作成
if (!currentLineText.trim()) {
const newTask = - [ ] ${now}- ;
this.app.workspace.activeEditor.editor.replaceRange(newTask, {line: initialCursorPos.line, ch: 0}, {line: initialCursorPos.line, ch: currentLineText.length});
this.app.workspace.activeEditor.editor.setCursor({line: initialCursorPos.line, ch: newTask.length});
return;
}
// 1. のみ、または タスク名 の場合: 開始時刻を追加
if (/^- \ \(?! \d{2}:\d{2}-)/.test(currentLineText)) { // タスク名があっても対応するように修正
const updatedLine = currentLineText.replace("- ", - [ ] ${now}-);
this.app.workspace.activeEditor.editor.replaceRange(updatedLine, {line: initialCursorPos.line, ch: 0}, {line: initialCursorPos.line, ch: currentLineText.length});
// カーソルをカーソル行の末尾に移動
this.app.workspace.activeEditor.editor.setCursor({line: initialCursorPos.line, ch: updatedLine.length});
}
// 2. hh:mm- の場合: 終了時刻を追加して、完了状態にする
else if (/^- \ \ \d{2}:\d{2}-/.test(currentLineText)) {
const updatedLine = currentLineText.replace(/^-\s\ \\s(\d{2}:\d{2})-/, - [x] $1-${now});
this.app.workspace.activeEditor.editor.replaceRange(updatedLine, {line: initialCursorPos.line, ch: 0}, {line: initialCursorPos.line, ch: currentLineText.length});
// カーソルを次の行に設定
this.app.workspace.activeEditor.editor.setCursor({line: initialCursorPos.line + 1, ch: 0});
}
// 3. x hh:mm-hh:mm の場合: 行を複製し、複製した行を未完了状態にして、hh:mm-hh:mm の部分を消す
else if (/^- \x\ \d{2}:\d{2}-\d{2}:\d{2}/.test(currentLineText)) {
const timeRangeEnd = currentLineText.indexOf(" ", 16);
const taskDescription = currentLineText.slice(timeRangeEnd).trim();
const newLine = - [ ] ${taskDescription};
// 現在の行を保持し、複製した行を追加
this.app.workspace.activeEditor.editor.replaceRange(currentLineText, {line: initialCursorPos.line, ch: 0}, {line: initialCursorPos.line, ch: currentLineText.length});
this.app.workspace.activeEditor.editor.replaceRange(newLine + "\n", {line: initialCursorPos.line + 1, ch: 0});
// カーソルを次の行の末尾に設定
this.app.workspace.activeEditor.editor.setCursor({line: initialCursorPos.line + 1, ch: newLine.length});
}
%>